var drvBehaviorAnalysis;
require(['grid', 'topBar', 'layout', 'date'], function () {
    var postData = null;
    	postData = {};
    var drvBhvAnalysisDialog,	
    	manyDrvBhvAnalysisDialog;
    var $gridList = $('#grid'),
        $top = $('#top');
    $('body').layout();
    $top.topBar({
        left: [{
            type: 'filter-more',
            dateType: 'custom',
            search: function () {
                if (getPostData()) {
                	if(postData.addDrvAnalysis!="1"){
                		$gridList.grid('hideCol',"drvName");
                	}else{
                		$gridList.grid('showCol',"drvName");
                	}
                    $gridList.grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }, {
                    text: '分析对象',
                    name: 'addDrvAnalysis',
                    type: 'item-select',
                    data: [
                        {text: '车辆', value: ''},
                        {text: '驾驶员', value: '1'}
                    ]
                }
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
				text: '对比分析',
				type: 'btn',
				click: function () {
					analysisData();
				}
			}
        ]
    });

    function exportFile(suffix) {
        if ($gridList.getGridParam("reccount") > 0 && getPostData()) {
            var url = "/drvBehaviorAnalysis/exportDrvBehaviorAnalysis" + suffix + ".htm";
            $.exportFile(url, postData);
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }

    $gridList.grid({
        url: "/drvBehaviorAnalysis/searchDrvBehaviorAnalysis.htm",
        shrinkToFit: false,
        //tableId:'8ec82ba949f84a048940d0e18be20ce7',
        colModel: [
            {name: "id", label: 'id', key: true, hidden: true},
            {
                name: "drvName", label: '驾驶员', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'drvName' + rowId + "\'";
                }
            },
            {
                name: "licPltNo", label: '车牌号', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'licPltNo' + rowId + "\'";
                }
            },
            {
                name: "carInCd", label: '车辆编号', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'carInCd' + rowId + "\'";
                }
            },
            {
                name: "lineName", label: '营运线路', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'lineName' + rowId + "\'";
                }
            },
            {
                name: "organName", label: '所属机构', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'organName' + rowId + "\'";
                }
            },
            {
                name: "rcrdTime", label: '运营日期', align: "center", width:120,
                cellattr: function (rowId) {
                    return 'id=\'rcrdTime' + rowId + "\'";
                }
            },
            {
                name: "totRngOprtDura", label: '运行时长', align: "center", width:120,
                cellattr: function (rowId) {
                    return 'id=\'totRngOprtDura' + rowId + "\'";
                }
            },
            {
                name: "totRngMile", label: '运行里程', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'totRngMile' + rowId + "\'";
                }
            },
            {
                name: "fuelTypeName", label: '燃料类型', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'fuelTypeName' + rowId + "\'";
                }
            },
            {
                name: "totRngEnerCons", label: '运行能耗', align: "right", width:80,
                cellattr: function (rowId) {
                    return 'id=\'totRngEnerCons' + rowId + "\'";
                }
            },
            {
                name: "enerConsMile", label: '百公里能耗(L|m³|KWh/100KM)', align: "right", width:80,
                cellattr: function (rowId) {
                    return 'id=\'enerConsMile' + rowId + "\'";
                }
            },
            {
                name: "allrank", label: '能耗排名', align: "right", width:80,
                cellattr: function (rowId) {
                    return 'id=\'allrank' + rowId + "\'";
                }
            },
            {name: "spedCnt", label: '超速次数',  align: "right", width:80, 
            	cellattr: function (rowId) {
                	return 'id=\'spedCnt' + rowId + "\'";
            	}
            },
            {name: "revupCount", label: '急加速次数', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'revupCount' + rowId + "\'";
	        	}
            },
            
            {name: "sharpSldnCount", label: '急减速次数', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'sharpSldnCount' + rowId + "\'";
	        	}
            },
            
            {name: "bhv7count", label: 'ABS次数', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'bhv7count' + rowId + "\'";
	        	}
            },
            
            {name: "bhv5count", label: '空档滑行次数', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'bhv5count' + rowId + "\'";
	        	}
            },
            {name: "sctBhvDura", label: '空档滑行时长', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'sctBhvDura' + rowId + "\'";
	        	}
            },
            {name: "ecnSpdOfMotVhiDura", label: '经济车速时长', align: "right", width:80,
            	cellattr: function (rowId) {
	            	return 'id=\'ecnSpdOfMotVhiDura' + rowId + "\'";
	        	}
            },
            {name: "drvBehaviorOperate", label: '图表分析', formatter: 'operArea', width: 80,
            	formatoptions: [{
                    text: '图表分析',
                    css:'operate-edit',
                    click: function (data) {
                    	getEditDialog('图表分析').dialog('exec', 'showCharts',data.lineId, data.organId, data.rcrdTime, data.spedCnt, data.revupCount,
                    		data.sharpSldnCount, data.bhv7count, data.bhv5count, data.lineName, data.organName, data.drvName, data.licPltNo,data.sctBhvDura,
                    		data.ecnSpdOfMotVhiDura, $('#top').parent().getValues().beginTime, $('#top').parent().getValues().endTime, 
                    		$('#top').parent().getValues().addDrvAnalysis);
                    }
                }]
            },
            {name : "lineId",index : "lineId",hidden: true},
			{name : "organId",index : "organId",hidden: true}
        ],
        gridComplete: function () {
        	var gridName = "grid";
            Merger(gridName, 'drvName');
            Merger(gridName, 'licPltNo');
            Merger(gridName, 'carInCd');
            Merger(gridName, 'lineName');
            Merger(gridName, 'organName');
            Merger(gridName, 'rcrdTime');
            Merger(gridName, 'allrank');
        }
    });

    //获取需要传递到后台的参数
    function getPostData() {
        postData = null;
        var type = 'LINE';
        var lineIds = commonFun.getLeftTreeCheckedNodes(type);
        if (lineIds == null || !lineIds.length) {
            $.messager.alert("提示", "请先选择左侧树的线路!", "warning");
            return false;
        }
        postData = $('#top').parent().getValues();
        postData.type = type;
        postData.orgIds = lineIds.toString();
        return true;
    }

    function getEditDialog(option) {
        option = option || {};
        if (drvBhvAnalysisDialog) {
        	drvBhvAnalysisDialog.dialog('open');
            if (option.title) {
            	drvBhvAnalysisDialog.dialog('setOption', 'title', option.title);
            }
        } else {
        	drvBhvAnalysisDialog = $('<div>').dialog({
                title: option.title || '图表分析',
                url: './energy/report/drvBehaviorAnalysisDialog.htm',
                height: 420,
                width: 770,
                autoOpen: true,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return drvBhvAnalysisDialog;
    }
    
    
    function getCompareDialog(option) {
//    	funName = flag;
        option = option || {};
        if (manyDrvBhvAnalysisDialog) {
        	manyDrvBhvAnalysisDialog.dialog('open');
            if (option.title) {
            	manyDrvBhvAnalysisDialog.dialog('setOption', 'title', option.title);
            }
        } else {
        	manyDrvBhvAnalysisDialog = $('<div>').dialog({
                title: option.title || '对比分析',
                url: './energy/report/manyDrvBehaviorAnalysisDialog.htm',
                height: 420,
                width: 770,
                autoOpen: true,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return manyDrvBhvAnalysisDialog;
    }
    
    
    function analysisData(){
		var rowIds = $gridList.grid("getGridParam", "selarrrow");
		if (rowIds.length == 0) {
			$.messager.alert("提示","请至少选择一个");
			return false;
		}
		var getResult = $gridList.getRowData(rowIds[0]);
		var resultArr = [];
		var drvOrlicPltNoArr = [];
		for(var i=0;i < rowIds.length; i++){
			var rowData = $gridList.getRowData(rowIds[i]);
			var tempArr=[];
			tempArr.push(parseFloat(rowData.spedCnt));
			tempArr.push(parseFloat(rowData.revupCount));
			tempArr.push(parseFloat(rowData.sharpSldnCount));
			tempArr.push(parseFloat(rowData.bhv7count));
			tempArr.push(parseFloat(rowData.bhv5count));
			resultArr.push(tempArr);
			if($('#top').parent().addDrvAnalysis!="1"){
				drvOrlicPltNoArr.push(rowData.licPltNo);
			}else{
				drvOrlicPltNoArr.push(rowData.drvName);
			}
		}
		
		getCompareDialog('对比分析').dialog('exec', 'showCharts', getResult.lineId, getResult.organId, getResult.rcrdTime,
				$('#top').parent().getValues().beginTime, $('#top').parent().getValues().endTime, $('#top').parent().getValues().addDrvAnalysis,
				resultArr, drvOrlicPltNoArr);
		
    }
    
    
    //公共调用方法
    function Merger(CellName) {
        //得到显示到界面的id集合
        var mya = $gridList.getDataIDs();
        //当前显示多少条
        var length = mya.length;
        for (var i = 0; i < length; i++) {
            //从上到下获取一条信息
            var before = $gridList.grid('getRowData', mya[i]);
            //如果不是混合动力就跳过
            if (before['carType'] != '混合动力车(新能源)') {
                rowSpanTaxCount = 1;
                continue;
            }
            //定义合并行数
            var rowSpanTaxCount = 1;
            for (var j = i + 1; j <= length; j++) {
                //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
                var end = $gridList.grid('getRowData', mya[j]);

                if (before[CellName] == end[CellName] && before['licPltNo'] == end['licPltNo']) {
                    rowSpanTaxCount++;
                    $gridList.setCell(mya[j], CellName, '', {display: 'none'});
                } else {
                    rowSpanTaxCount = 1;
                    break;
                }
                $("#" + CellName + "" + mya[i] + "").attr("rowspan", rowSpanTaxCount);
            }
        }
    }
});
